﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.IO;

public partial class admin_Students_ListStudents : System.Web.UI.Page
{
    StudentBLL data = new StudentBLL();
    ClassBLL classData = new ClassBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["url_back_admin"] = Request.RawUrl;

            LoadCategory();
            LoadCategoryDM();
            ShowData();
            
        }
    }

    private void ShowChildDM(int parentId, string space)
    {
        List<Category> listDataSource = classData.GetByParent(parentId, true);

        foreach (Category item in listDataSource)
        {
            ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
            DropDownList1.Items.Add(obj);
            ShowChildDM(item.Id, space + "- - - ");
        }
    }

    private void ShowChildDS(int parentId, string space)
    {
        List<Class> listDataSource1 = classData.GetByParentClass(parentId);
        foreach (Class item in listDataSource1)
        {
            ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
            dropParent.Items.Add(obj);
            ShowChildDS(item.Id, space + "- - - ");
        }
    }

    private void LoadCategory()
    {
        dropParent.Items.Clear();
        ListItem itemRoot = new ListItem("Danh sách lớp học", "0");
        dropParent.Items.Insert(0, itemRoot);

    }

    private void LoadCategoryDM()
    {
        DropDownList1.Items.Clear();
        ListItem itemRoot = new ListItem("Danh mục lớp học", "0");
        DropDownList1.Items.Insert(0, itemRoot);
        ShowChildDM(14, " - - ");

    }

    private void ShowData()
    {
        int parentId = Convert.ToInt32(dropParent.SelectedValue);
        int classId = Convert.ToInt32(DropDownList1.SelectedValue);
        if (parentId == 0)
        {
            

            if (dropIsEnable.SelectedIndex == 0)
            {
                if (classId != 0)
                    gridView.DataSource = data.GetByCateParentId(classId);
                else
                    gridView.DataSource = data.GetAll();
            }
            else
            {
                bool isEnable = true;
                if (dropIsEnable.SelectedValue == "2") isEnable = false;

                if (parentId != 0)
                    gridView.DataSource = data.GetByCateParentId(classId, isEnable);
                else
                    gridView.DataSource = data.GetAll(isEnable);
            }
        }
        else
        {
            if (dropIsEnable.SelectedIndex == 0)
            {
                if (parentId != 0)
                    gridView.DataSource = data.GetByCategory(parentId);
                else
                    gridView.DataSource = data.GetByCateParentId(classId);
            }
            else
            {
                bool isEnable = true;
                if (dropIsEnable.SelectedValue == "2") isEnable = false;

                if (parentId != 0)
                    gridView.DataSource = data.GetByCategory(parentId, isEnable);
                else
                    gridView.DataSource = data.GetByCateParentId(classId, isEnable);
            }
        }
        
        gridView.DataBind();

    }

    private void Delete_Dequy(int id)
    {    
        List<Student> listDataSource = data.GetByCategory(id);
        foreach (Student item in listDataSource)
        {
            if (item.Images != "")
            {
                Myfunction.DeleteFile("Images/Students", item.Images);
            }

            Delete_Dequy(item.Id);
        }
        data.Delete(id);
    }

    private void Enable(bool isEnable)
    {
        foreach (GridViewRow item in gridView.Rows)
        {
            foreach (Control ctrl in item.Cells[1].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    if (check.Checked)
                    {
                        int id = Convert.ToInt32(item.Cells[2].Text);
                        data.Enable(id, isEnable);
                    }
                    break;
                }
            }
        }
        ShowData();
    }
    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowIndex >= 0) //Bo qua dong dau
            {
                int id = Convert.ToInt32(e.Row.Cells[2].Text);
                if (e.Row.Cells[4].Text == "0") e.Row.Cells[4].Text = "Danh mục lớp";
                else e.Row.Cells[4].Text = classData.GetById(Convert.ToInt32(e.Row.Cells[4].Text)).Name;
                string firstname = data.GetById(Convert.ToInt32(e.Row.Cells[2].Text)).FirstName;
                string lastname =  data.GetById(Convert.ToInt32(e.Row.Cells[2].Text)).LastName;
                e.Row.Cells[3].Text = lastname.Trim() + " " + firstname.Trim();
                bool isEnable = Convert.ToBoolean(data.GetById(id).isEnable);

                //Icon check
                Image img = new Image();
                foreach (Control item in e.Row.Cells[7].Controls)
                {
                    if (item.GetType() == typeof(Image))
                    {
                        img = (Image)item;
                        break;
                    }
                }
                if (isEnable)
                    img.ImageUrl = "~/Images/check.png";
                else
                {
                    img.ImageUrl = "~/Images/an.png";

                }

            }
        }
        catch { }
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        lbThongBao.Text = "";
        try
        {
            gridView.PageIndex = e.NewPageIndex;
            ShowData();
        }
        catch { }
    }
    protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[2].Text);
            Student students = data.GetById(id);
            //delete image
            Myfunction.DeleteFile("Images/Students", students.Images);
            Delete_Dequy(id);
            Myfunction.ThongBao(lbThongBao, "Danh mục đã được xóa");

            LoadCategory();
            ShowData();
        }
        catch (Exception ex)
        {
            Myfunction.ThongBao(lbThongBao, ex.Message);
        }
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/Students/DetailsStudents.aspx");
    }
    protected void btnVisible_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        lbThongBao.Text = "";
        Enable(false);
    }
    protected void btnEnable_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        lbThongBao.Text = "";
        Enable(true);
    }
    protected void btnXoaChon_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            foreach (GridViewRow item in gridView.Rows)
            {
                foreach (Control ctrl in item.Cells[1].Controls)
                {
                    if (ctrl.GetType() == typeof(CheckBox))
                    {
                        CheckBox check = (CheckBox)ctrl;
                        if (check.Checked)
                        {
                            int id = Convert.ToInt32(item.Cells[2].Text);
                            Delete_Dequy(id);
                        }
                        break;
                    }
                }
            }
            LoadCategory();
            ShowData();
        }
        catch (Exception ex)
        {
            Myfunction.ThongBao(lbThongBao, ex.Message);
        }
    }
    protected void linkCheckAll_Click(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        foreach (GridViewRow item in gridView.Rows)
        {
            foreach (Control ctrl in item.Cells[1].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    check.Checked = true;
                    break;
                }
            }
        }
    }
    protected void linkUnCheckAll_Click(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        foreach (GridViewRow item in gridView.Rows)
        {
            foreach (Control ctrl in item.Cells[1].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    check.Checked = false;
                    break;
                }
            }
        }
    }
    protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        ShowData();
    }
    protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
        ShowData();
    }
    protected void dropParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        ShowData();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int parentId = Convert.ToInt32(DropDownList1.SelectedValue);
        LoadCategory(parentId);
        ShowData();
    }

    private void LoadCategory(int parentId)
    {
        dropParent.Items.Clear();
        
        ListItem itemRoot = new ListItem("Danh sách lớp học", "0");
        dropParent.Items.Insert(0, itemRoot);
        ShowChildDS(parentId, " - - ");
    }
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
         "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        // Bỏ phân trang - Nếu chỉ muỗn Export Trang hiện hành thì chọn =true
        gridView.AllowPaging = false;
        gridView.DataBind();
        gridView.RenderControl(hw);
        //Thay đổi Style
        string style = @"";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();

    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //Xác nhận điều khiển HtmlForm tại thời gian chạy ASP.NET
    }
}